/**
 *
 * @author Erivaldo Lourenco
 * @author Juliana Cavalcanti
 * 
 */

package br.ufal.ic.p3.dao;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

public class BuscaDao {

	private DaoUtilidades daoUtilidades;

	public BuscaDao() {
		daoUtilidades = new DaoUtilidades();
	}

	public ArrayList<String> buscaFuncionarioCPF(String cpf) {
		ArrayList<String> array = new ArrayList<String>();
		Connection conn = null;
		java.sql.Statement stmt = null;

		try {
			conn = daoUtilidades.getConection();

			stmt = conn.createStatement();

			ResultSet rs = stmt
					.executeQuery("SELECT cpf, nome, sobrenome FROM FUNCIONARIO WHERE cpf = '"
							+ cpf + "'");

			while (rs.next()) {
				String x = rs.getString("nome");
				String s = rs.getString("sobrenome");
				String f = rs.getString("cpf");
				array.add(0,x);
				array.add(1,s);
				array.add(2,f);
			}

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return array;
	}

	public void buscaFuncionarioNome(String nome) {

		Connection conn = null;
		java.sql.Statement stmt = null;

		try {
			conn = daoUtilidades.getConection();

			stmt = conn.createStatement();

			ResultSet rs = stmt
					.executeQuery("SELECT cpf,nome,sobrenome FROM FUNCIONARIO WHERE nome = '"
							+ nome + "'");

			while (rs.next()) {
				String x = rs.getString("nome");
				String s = rs.getString("sobrenome");
				String f = rs.getString("cpf");
				System.out.println("Nome = " + x + "\nSobrenome = " + s
						+ "\nCPF = " + f);
			}

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	public int verificaFuncionario(String cpf) {
		Connection conn = null;
		java.sql.Statement stmt = null;

		try {
			conn = daoUtilidades.getConection();

			stmt = conn.createStatement();

			ResultSet rs = stmt
					.executeQuery("SELECT cpf FROM FUNCIONARIO WHERE cpf = '"
							+ cpf + "'");
			if (rs.next() || cpf == "") {
				return 1; // CPF cadastrado
			} else {
				return 0;// CPF nao cadastrado
			}

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return 0;
	}

	//

	public ArrayList<String> buscaClienteCPF(String cpf) {
		
		ArrayList<String> array = new ArrayList<String>();

		Connection conn = null;
		java.sql.Statement stmt = null;

		try {
			conn = daoUtilidades.getConection();

			stmt = conn.createStatement();

			ResultSet rs = stmt
					.executeQuery("SELECT cpf, nome, sobrenome FROM CLIENTE WHERE cpf = '"
							+ cpf + "'");

			while (rs.next()) {
				String x = rs.getString("nome");
				String s = rs.getString("sobrenome");
				String f = rs.getString("cpf");
				System.out.println("Nome = " + x + "\nSobrenome = " + s
						+ "\nCPF = " + f);
			}

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return array;

	}

	public void buscaClienteNome(String nome) {

		Connection conn = null;
		java.sql.Statement stmt = null;

		try {
			conn = daoUtilidades.getConection();

			stmt = conn.createStatement();

			ResultSet rs = stmt
					.executeQuery("SELECT cpf, nome, sobrenome FROM CLIENTE WHERE nome = '"
							+ nome + "'");

			while (rs.next()) {
				String x = rs.getString("nome");
				String s = rs.getString("sobrenome");
				String f = rs.getString("cpf");
				System.out.println("Nome = " + x + "\nSobrenome = " + s
						+ "\nCPF = " + f);
			}

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	public int verificaCliente(String cpf) {
		Connection conn = null;
		java.sql.Statement stmt = null;

		try {
			conn = daoUtilidades.getConection();

			stmt = conn.createStatement();

			ResultSet rs = stmt
					.executeQuery("SELECT cpf FROM CLIENTE WHERE cpf = '" + cpf
							+ "'");

			if (rs.next() || cpf == "") {
				return 1; // CPF cadastrado
			}

			else {
				return 0;// CPF nao cadastrado
			}

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return 0;
	}

	//

	public ArrayList<String> buscaClienteJuridicoCNPJ(String cnpj) {
		
		ArrayList<String> array = new ArrayList<String>();

		Connection conn = null;
		java.sql.Statement stmt = null;

		try {
			conn = daoUtilidades.getConection();

			stmt = conn.createStatement();

			ResultSet rs = stmt
					.executeQuery("SELECT cnpj, razao_social FROM JURIDICO WHERE cnpj = '"
							+ cnpj + "'");

			while (rs.next()) {
				String x = rs.getString("razao_social");
				String f = rs.getString("cnpj");
				System.out.println("Razao Social = " + "\nCNPJ = " + f);
			}

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return array;
	}

	public void buscaClienteJuridicoRazaoSocial(String razaoSocial) {

		Connection conn = null;
		java.sql.Statement stmt = null;

		try {
			conn = daoUtilidades.getConection();

			stmt = conn.createStatement();

			ResultSet rs = stmt
					.executeQuery("SELECT cnpj, razao_social FROM JURIDICO WHERE razao_social = '"
							+ razaoSocial + "'");

			while (rs.next()) {
				String x = rs.getString("razao_social");
				String f = rs.getString("cnpj");
				System.out.println("Razao Social = " + x + "\nCNPJ = " + f);
			}

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	public int verificaClienteJuridico(String cnpj) {
		Connection conn = null;
		java.sql.Statement stmt = null;

		try {
			conn = daoUtilidades.getConection();

			stmt = conn.createStatement();

			ResultSet rs = stmt
					.executeQuery("SELECT cnpj FROM JURIDICO WHERE cnpj = '"
							+ cnpj + "'");

			if (rs.next() || cnpj == "") {
				return 1; // CNPJ cadastrado
			}

			else {
				return 0;// CNPJ nao cadastrado
			}

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return 0;
	}
}
